package com.ruoyi.web.controller.dgw;

import java.util.List;
import java.util.Map;

import com.ruoyi.dgw.domain.TagNumber;
import com.ruoyi.dgw.domain.TagNumberExecl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dgw.domain.RegistrationFrom;
import com.ruoyi.dgw.service.IRegistrationFromService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 养殖户信息登录Controller
 *
 * @author ll
 * @date 2022-01-02
 */
@RestController
@RequestMapping("/dgw/registration")
@Slf4j
public class RegistrationFromController extends BaseController {
    @Autowired
    private IRegistrationFromService registrationFromService;

    /**
     * 查询养殖户信息登录列表
     */
    @PreAuthorize("@ss.hasPermi('dgw:registration:list')")
    @GetMapping("/list")
    public TableDataInfo list(RegistrationFrom registrationFrom) {
        startPage();
        List<RegistrationFrom> list = registrationFromService.selectRegistrationFromList(registrationFrom);
        return getDataTable(list);
    }

    /**
     * 导出养殖户信息登录列表
     */
    @PreAuthorize("@ss.hasPermi('dgw:registration:export')")
    @Log(title = "养殖户信息登录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RegistrationFrom registrationFrom) {
        List<RegistrationFrom> list = registrationFromService.selectRegistrationFromList(registrationFrom);
        ExcelUtil<RegistrationFrom> util = new ExcelUtil<RegistrationFrom>(RegistrationFrom.class);
        return util.exportExcel(list, "养殖户信息登录数据");
    }

    @Log(title = "养殖户信息登录表记录", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('dgw:registration:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RegistrationFrom> util = new ExcelUtil<RegistrationFrom>(RegistrationFrom.class);
        List<RegistrationFrom> userList = util.importExcel(file.getInputStream());
        log.info("用户数据：{}", userList);
        String operName = getUsername();
        registrationFromService.importRegistrationFrom(userList, operName);
        return AjaxResult.success("操作成功");
    }

    @GetMapping("/importTemplate")
    @Log(title = "下载模板", businessType = BusinessType.INSERT)
    public AjaxResult importTemplate() {
        ExcelUtil<RegistrationFrom> util = new ExcelUtil<RegistrationFrom>(RegistrationFrom.class);
        log.info("下载模板：{}", util);
        return util.importTemplateExcel("用户数据");
    }

    /**
     * 获取养殖户信息登录详细信息
     */
    @PreAuthorize("@ss.hasPermi('dgw:registration:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(registrationFromService.selectRegistrationFromById(id));
    }

    /**
     * 新增养殖户信息登录
     */
    @PreAuthorize("@ss.hasPermi('dgw:registration:add')")
    @Log(title = "养殖户信息登录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RegistrationFrom registrationFrom) {
        return toAjax(registrationFromService.insertRegistrationFrom(registrationFrom));
    }

    /**
     * 修改养殖户信息登录
     */
    @PreAuthorize("@ss.hasPermi('dgw:registration:edit')")
    @Log(title = "养殖户信息登录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegistrationFrom registrationFrom) {
        return toAjax(registrationFromService.updateRegistrationFrom(registrationFrom));
    }

    /**
     * 删除养殖户信息登录
     */
    @PreAuthorize("@ss.hasPermi('dgw:registration:remove')")
    @Log(title = "养殖户信息登录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(registrationFromService.deleteRegistrationFromByIds(ids));
    }

    @GetMapping("/queryVillage")
    public TableDataInfo queryVillage(String villagesTowns) {
        log.info("接收参数：{}", villagesTowns);
        startPage();
        List<Map<String, String>> list = registrationFromService.queryVillage(villagesTowns);
        return getDataTable(list);
    }

    @GetMapping("/queryUser")
    public TableDataInfo queryUser(String village) {
        log.info("接收参数：{}", village);
        startPage();
        List<RegistrationFrom> list = registrationFromService.queryUser(village);
        return getDataTable(list);
    }

    @GetMapping("/queryVSum")
    public AjaxResult queryVSum(String villagesTowns, String village) {

        Map<String, String> list = registrationFromService.queryVSum(villagesTowns, village);
        log.info("查询：{}", list);
        return AjaxResult.success(list);
    }

    @GetMapping("/queryUserName")
    public AjaxResult queryUserName(String village) {
        List<RegistrationFrom> list = registrationFromService.queryUserName(village);
        return AjaxResult.success(list);
    }

}
